


package ciicos.equ.controller;

import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.equ.entity.EquBaseEntity;
import ciicos.equ.entity.UpkeepRecordEntity;
import ciicos.equ.model.upkeeprecord.UpkeepRecordForm;
import ciicos.equ.model.upkeeprecord.UpkeepRecordInfoVO;
import ciicos.equ.model.upkeeprecord.UpkeepRecordListVO;
import ciicos.equ.model.upkeeprecord.UpkeepRecordPagination;
import ciicos.equ.service.EquBaseService;
import ciicos.equ.service.UpkeepRecordService;
import ciicos.exception.DataException;
import ciicos.permission.entity.UserEntity;
import ciicos.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * upkeepRecord
 *
 * @版本： V3.2.0
 * @版权：
 * @作者： 企业研发中心
 * @日期： 2024-03-12
 */
@Slf4j
@RestController
@Api(tags = "upkeepRecord", value = "upkeepRecord")
@RequestMapping("/api/upkeepRecord/UpkeepRecord")
public class UpkeepRecordController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private UpkeepRecordService upkeepRecordService;


    /**
     * 列表
     *
     * @param upkeepRecordPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody UpkeepRecordPagination upkeepRecordPagination) throws IOException {
        //树形判断是否有参数，有参数不造树
        List<UpkeepRecordEntity> list = upkeepRecordService.getList(upkeepRecordPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<UpkeepRecordListVO> listVO = JsonUtil.getJsonToList(list, UpkeepRecordListVO.class);
        for (UpkeepRecordListVO upkeepRecordVO : listVO) {
            ActionResult info = info(upkeepRecordVO.getId());
            Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
            upkeepRecordVO.setEquname_name(generaterSwapUtil.getDynName("529567261959131013", "F_Name", "F_Id", "", upkeepRecordVO.getEquname(), false, true, "[]", -1, dataMap));

            upkeepRecordVO.setEqucode_name(upkeepRecordVO.getEqucode());

            upkeepRecordVO.setUpkeepuserid_name(generaterSwapUtil.usersSelectValues(upkeepRecordVO.getUpkeepuserid()));

            upkeepRecordVO.setUpkeeptime_name(upkeepRecordVO.getUpkeeptime() != null ?
                    new Date(upkeepRecordVO.getUpkeeptime()) : null);

            upkeepRecordVO.setUpkeeptype_name(generaterSwapUtil.getDicName(upkeepRecordVO.getUpkeeptype(), "537180778887388165"));

            upkeepRecordVO.setUpkeepresult_name(generaterSwapUtil.getDicName(upkeepRecordVO.getUpkeepresult(), "537180834780683269"));

            upkeepRecordVO.setUpkeepcontent_name(upkeepRecordVO.getUpkeepcontent());

            upkeepRecordVO.setRecordtime_name(upkeepRecordVO.getRecordtime());

            upkeepRecordVO.setFile_name(generaterSwapUtil.getFileNameInJson(upkeepRecordVO.getEqufile()));

            upkeepRecordVO.setEqupic_name(generaterSwapUtil.getFileNameInJson(upkeepRecordVO.getEqupic()));

            upkeepRecordVO.setCreatoruserid_name(generaterSwapUtil.userSelectValue(upkeepRecordVO.getCreatoruserid()));

            upkeepRecordVO.setCreatortime_name(upkeepRecordVO.getCreatortime());

            upkeepRecordVO.setLastupdateuserid_name(generaterSwapUtil.userSelectValue(upkeepRecordVO.getLastupdateuserid()));

            upkeepRecordVO.setLastupdatetime_name(upkeepRecordVO.getLastupdatetime());

            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(upkeepRecordPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    @Autowired
    private EquBaseService equBaseService;

    /**
     * 创建
     *
     * @param upkeepRecordForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid UpkeepRecordForm upkeepRecordForm) throws DataException {
        String b = upkeepRecordService.checkForm(upkeepRecordForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        }
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        upkeepRecordForm.setCreatoruserid(userInfo.getUserId());
        upkeepRecordForm.setCreatortime(DateUtil.getNow());
        UpkeepRecordEntity entity = JsonUtil.getJsonToBean(upkeepRecordForm, UpkeepRecordEntity.class);
        entity.setId(mainId);
        upkeepRecordService.save(entity);
        //获取设备对象
        EquBaseEntity equInfo = equBaseService.getInfo(entity.getEquname());
        if (equInfo != null) {
            //获取保养周期
            double upkeepCycle = equInfo.getUpkeepCycle();
            int hour = (int) upkeepCycle + 24;
            //保养日期
            LocalDateTime localDateTime = DateUtil.DateHourAccumulator(entity.getUpkeeptime(), hour);
            Date localDateTimeToDate = DateUtil.localDateTimeToDate(localDateTime);
            equInfo.setNextUpkeepDate(localDateTimeToDate);
            equBaseService.updateById(equInfo);
        }

        return ActionResult.success("创建成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/batchRemove/{ids}")
    @Transactional
    @ApiOperation("批量删除")
    public ActionResult batchRemove(@PathVariable("ids") String ids) {
        String[] idList = ids.split(",");
        int i = 0;
        for (String allId : idList) {
            this.delete(allId);
            i++;
        }
        if (i == 0) {
            return ActionResult.fail("删除失败");
        }
        return ActionResult.success("删除成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<UpkeepRecordInfoVO> info(@PathVariable("id") String id) {
        UpkeepRecordEntity entity = upkeepRecordService.getInfo(id);
        UpkeepRecordInfoVO vo = JsonUtil.getJsonToBean(entity, UpkeepRecordInfoVO.class);
        vo.setCreatoruserid(generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));
        if (vo.getCreatortime() != null) {
            vo.setCreatortime(vo.getCreatortime());
        }
        vo.setLastupdateuserid(generaterSwapUtil.userSelectValue(vo.getLastupdateuserid()));
        if (vo.getLastupdatetime() != null) {
            vo.setLastupdatetime(vo.getLastupdatetime());
        }

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<UpkeepRecordInfoVO> detailInfo(@PathVariable("id") String id) {
        ActionResult info = info(id);
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
        UpkeepRecordEntity entity = upkeepRecordService.getInfo(id);
        UpkeepRecordInfoVO vo = JsonUtil.getJsonToBean(entity, UpkeepRecordInfoVO.class);

        //子表数据转换

        //附表数据转换

//添加到详情表单对象中
        vo.setEquname(generaterSwapUtil.getDynName("529567261959131013", "F_Name", "F_Id", "", vo.getEquname(), false, true, "[]", -1, dataMap));

        vo.setEqucode(vo.getEqucode());

        vo.setUpkeepuserid(generaterSwapUtil.usersSelectValues(vo.getUpkeepuserid()));

        vo.setUpkeeptime(vo.getUpkeeptime());

        vo.setUpkeeptype(generaterSwapUtil.getDicName(vo.getUpkeeptype(), "537180778887388165"));

        vo.setUpkeepresult(generaterSwapUtil.getDicName(vo.getUpkeepresult(), "537180834780683269"));

        vo.setUpkeepcontent(vo.getUpkeepcontent());

        vo.setRecordtime(vo.getRecordtime());

        vo.setEqufile(generaterSwapUtil.getFileNameInJson(vo.getEqufile()));

        vo.setEqupic(generaterSwapUtil.getFileNameInJson(vo.getEqupic()));

        vo.setCreatoruserid(generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));

        vo.setCreatortime(vo.getCreatortime());

        vo.setLastupdateuserid(generaterSwapUtil.userSelectValue(vo.getLastupdateuserid()));

        vo.setLastupdatetime(vo.getLastupdatetime());


        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param upkeepRecordForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid UpkeepRecordForm upkeepRecordForm) throws DataException {
        String b = upkeepRecordService.checkForm(upkeepRecordForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        }
        UserInfo userInfo = userProvider.get();
        UpkeepRecordEntity entity = upkeepRecordService.getInfo(id);
        if (entity != null) {
            upkeepRecordForm.setLastupdateuserid(userInfo.getUserId());
            upkeepRecordForm.setLastupdatetime(DateUtil.getNow());
            UpkeepRecordEntity subentity = JsonUtil.getJsonToBean(upkeepRecordForm, UpkeepRecordEntity.class);
            subentity.setCreatoruserid(entity.getCreatoruserid());
            subentity.setCreatortime(entity.getCreatortime());
            boolean b1 = upkeepRecordService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        UpkeepRecordEntity entity = upkeepRecordService.getInfo(id);
        if (entity != null) {
            upkeepRecordService.delete(entity);
        }
        return ActionResult.success("删除成功");
    }


}
